﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using System.Drawing.Design;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

 
using DotSpatial.Data;

using DotSpatial.Topology;

namespace HaGisPack
{
    [Serializable ]
   public  class FDGridLayer:FELineLayer 
    {

        public FDGridLayer(string id)
            : base(id)
        {

            this.m_bShapeEditable = false;



            this._XCells = 100;
            this._YCells = 100;
            this._XUnit = 10;
            this._YUnit = 10;
            this._LeftBottomX = -500;
            this._LeftBottomY = -500;

            this._OffsetX = 0;
            this._OffsetY = 0;


            //标记其为不可选择
            this.BSelectable = false;

            
        }

       //需要标明的属性包括 偏移量，标注，坐标轴，左下角坐标值，网格数等得呢个

       # region 字段

       private int _XCells = 10;
       private int _YCells = 10;
       private double _LeftBottomX = -500;
       private double _LeftBottomY = -500;
       private double _XUnit = 1;
       private double _YUnit = 1;

       private bool _IsShowXLine = true;
       private bool _IsShowYLine = true;

       private double _OffsetX = 0;
       private double _OffsetY = 0;

       private bool _IsShowLeftAxis = true;

       private bool _IsShowBottomAxis = true;
       private bool _IsShowRightAxis = false;
       private bool _IsShowUpAxis = false;

       private int _mainScaleStep = 10;
       private int _viceScaleStep = 1;
       private double _mainScaleLength = 20;
       private double _viceScaleLength = 10;

       # endregion

       # region 属性

       /// <summary>
       /// X轴单元格数")]
       /// </summary>
       [Category ("特定设置"),DisplayName ("X轴单元格数")]
       public int XCells
       {
           get { return this._XCells; }
           set
           {
               if (value < 0)
               {
                   value = 0;
                }
               this._XCells = value;
               if (this.BRunning)
                   this.Redraw();
           }
       
       }

       /// <summary>
       /// Y轴单元格数
       /// </summary>
       [Category ("特定设置"),DisplayName ("Y轴单元格数")]
       public int YCells
       {
           get { return this._YCells; }
           set
           {
               if (value < 0)
               { 
                   value = 0; 
               }
               this._YCells = value;
               if (this.BRunning)
                   this.Redraw();
           }
       }

       /// <summary>
       /// X轴单元格长度
       /// </summary>
       [Category ("特定设置"),DisplayName ("X轴单元格长度")]
       public double XUnit
       {
           get { return this._XUnit; }
           set
           {
               if (value < 0)
                   value = 0;
               this._XUnit = value;
               if (this.BRunning)
                   this.Redraw();
           }
       }

       /// <summary>
       /// Y轴单元格长度
       /// </summary>
       [Category("特定设置"), DisplayName("Y轴单元格长度")]
       public double YUnit
       {
           get { return this._YUnit; }
           set
           {
               if (value < 0)
                   value = 0;
               this._YUnit = value;
               if (this.BRunning)
                   this.Redraw();
           }
       }

       /// <summary>
       /// 左下角点绝对坐标X
       /// </summary>
       [Category("特定设置"),DisplayName("左下角点绝对坐标X")]
       public double LeftBottomX
       {
           get { return this._LeftBottomX; }
           set
           {
               this._LeftBottomX = value;
               if (this.BRunning)
                   this.Redraw();
           }
       }


       /// <summary>
       /// 左下角点绝对坐标Y
       /// </summary>
       [Category("特定设置"), DisplayName("左下角点绝对坐标Y")]
       public double LeftBottomY
       {
           get { return this._LeftBottomY; }
           set
           {
               this._LeftBottomY = value;
               if (this.BRunning)
                   this.Redraw();
           }
       }

       /// <summary>
       /// 显示横向网格线
       /// </summary>
       [Category ("特定设置"),DisplayName ("显示横向网格线")]
       public bool IsShowXLine
       {
           get { return this._IsShowXLine; }
           set 
           {
               this._IsShowXLine = value;
               if (this.BRunning)
                   this.Redraw();
           }
       
       }

       /// <summary>
       /// 显示纵向网格线
       /// </summary>
       [Category ("特定设置"),DisplayName ("显示纵向网格线")]
       public bool IsShowYLine
       {
           get { return this._IsShowYLine; }
           set 
           {
               this._IsShowYLine = value;
               if (this.BRunning)
                   this.Redraw();
           }
       }

       /// <summary>
       /// 相对坐标相对于绝对坐标在X方向的偏移量
       /// </summary>
       [Browsable (false)]
       public double OffsetX
       {
           get { return this._OffsetX; }
           set 
           {
               this._OffsetX = value;
               if (this.BRunning)
                   this.Redraw();
           }
       }

       /// <summary>
       /// 相对坐标相对于绝对坐标在Y方向的偏移量
       /// </summary>
       [Browsable (false )]
       public double OffsetY
       {
           get { return this._OffsetY; }
           set 
           {
               this._OffsetY = value;
               if (this.BRunning)
                   this.Redraw();
           }
       }


       /// <summary>
       /// 自定义标注不可见
       /// </summary>
       [Browsable(false)]
       public override object LabelLayer
       {
           get
           {
               return base.LabelLayer;
           }
           set
           {
               base.LabelLayer = value;
           }
       }

        /// <summary>
        /// 不可选择，所以所选图形的样式设置属性不可见
        /// </summary>
       [Browsable(false)]
       public override object SelectionSymbol
       {
           get
           {
               return base.SelectionSymbol;
           }
           set
           {
               base.SelectionSymbol = value;
           }
       }

        /// <summary>
       /// 是否显示左侧坐标
        /// </summary>
        [Category ("坐标轴设置"),DisplayName ("是否显示左侧坐标")]
       public bool IsShowLeftAxis
       {
           get { return _IsShowLeftAxis; }
           set
           { 
               _IsShowLeftAxis = value;
               this.m_ParenLManager.CurrentMap.Refresh();
           }
       }

        /// <summary>
        /// 是否显示底部坐标
        /// </summary>
        [Category("坐标轴设置"), DisplayName("是否显示底部坐标")]
        public bool IsShowBottomAxis
        {
            get { return _IsShowBottomAxis; }
            set 
            { 
                _IsShowBottomAxis = value;
                this.m_ParenLManager.CurrentMap.Refresh();
            }
        }

        /// <summary>
        /// 是否显示右侧坐标
        /// </summary>
        [Category("坐标轴设置"), DisplayName("是否显示右侧坐标")]
        public bool IsShowRightAxis
        {
            get { return _IsShowRightAxis; }
            set
            { 
                _IsShowRightAxis = value;
                this.m_ParenLManager.CurrentMap.Refresh();
            }
        }

        /// <summary>
        /// 是否显示顶部坐标
        /// </summary>
        [Category("坐标轴设置"), DisplayName("是否显示顶部坐标")]
        public bool IsShowUpAxis
        {
            get { return _IsShowUpAxis; }
            set
            {
                _IsShowUpAxis = value;
                this.m_ParenLManager.CurrentMap.Refresh();
            }
        }

        /// <summary>
        /// 主刻度网格间距个数
        /// </summary>
        [Category("坐标轴刻度设置"), DisplayName("主刻度网格间距个数")]
        public int MainScaleStep
        {
            get { return _mainScaleStep; }
            set 
            {
                _mainScaleStep = value;
                this.m_ParenLManager.CurrentMap.Refresh();
            }
        }

        /// <summary>
        /// 副刻度网格间距个数
        /// </summary>
        [Category("坐标轴刻度设置"), DisplayName("副刻度网格间距个数")]
        public int ViceScaleStep
        {
            get { return _viceScaleStep; }
            set 
            {
                _viceScaleStep = value;
                this.m_ParenLManager.CurrentMap.Refresh();
            }
        }


        /// <summary>
        /// 主刻度线长度
        /// </summary>
        [Category("坐标轴刻度设置"), DisplayName("主刻度线长度")]
        public double MainScaleLength
        {
            get { return _mainScaleLength; }
            set 
            {
                _mainScaleLength = value;
                this.m_ParenLManager.CurrentMap.Refresh();
            }
        }

        /// <summary>
        /// 副刻度线长度
        /// </summary>
        [Category("坐标轴刻度设置"), DisplayName("副刻度线长度")]
        public double ViceScaleLength
        {
            get { return _viceScaleLength; }
            set 
            {
                _viceScaleLength = value;
                this.m_ParenLManager.CurrentMap.Refresh();
            }
        }


       # endregion


       /// <summary>
       /// 用于构建网格的线条feature
       /// </summary>
       protected void BuildGridLine()
       {
 
       }

       protected void BuildAxix()
       {
 
       }


       protected void DrawGridLine()
       {

           if ((this.ILayer as DotSpatial.Controls.MapLineLayer).Buffer == null)
           {
               (this.ILayer as DotSpatial.Controls.MapLineLayer).Buffer = new System.Drawing.Bitmap((int)(this.ILayer as DotSpatial.Controls.MapLineLayer).Extent.ToEnvelope().Width, (int)(this.ILayer as DotSpatial.Controls.MapLineLayer).Extent.ToEnvelope().Height);
               
           }

           this.g = System.Drawing.Graphics.FromImage((this.ILayer as DotSpatial.Controls.MapLineLayer).Buffer);

           this.r = new System.Drawing.Rectangle(0, 0, (int)this.m_ParenLManager.CurrentMap.MapFrame.ViewExtents.Width, (int)this.m_ParenLManager.CurrentMap.MapFrame.ViewExtents.Height);


         
           //所有横线左侧X坐标
           double LeftX = this._LeftBottomX + this._OffsetX;
           //所有横线右侧X坐标
           double RightX = this._LeftBottomX + this._OffsetX + this._XUnit * this._XCells;
           //所有纵线底部Y坐标
           double BottomY = this._LeftBottomY + this._OffsetY;
           //所有纵线顶部Y坐标
           double TopY = this._LeftBottomY + this._OffsetY + this._YUnit * this._YCells;

           //定义一个Shape足够了吧！！！！！！！！
           Shape shp = new Shape(FeatureType.Line);
           
           
           //有_YCells个网格，所以有_YCells+1个横线
           for (int i = 0; i < this._YCells + 1; i++)
           {
               Coordinate[] c = new Coordinate[2];
               c[0] = new Coordinate(LeftX, BottomY + this._YUnit * i);
               c[1] = new Coordinate(RightX, BottomY + this._YUnit * i);
                              
               shp.AddPart(c, CoordinateType.Regular);                             
           }


           //有_XCells个网格，所以有_XCells+1个纵线
           for (int i = 0; i < this._XCells + 1; i++)
           {
               Coordinate[] c = new Coordinate[2];
               c[0] = new Coordinate(LeftX + this._XUnit * i, BottomY);
               c[1] = new Coordinate(LeftX + this._XUnit * i, TopY);

               shp.AddPart(c, CoordinateType.Regular);
           }

           //一个Feature也足够了
           Feature f = new Feature(shp);
           if (f != null)
               (this.ILayer.DataSet as IFeatureSet).AddFeature(f);
           
                     
       }


       private DotSpatial.Controls.IMapLineLayer _tempLayer;
       System.Drawing.Graphics g;
       System.Drawing.Rectangle r;
        
      
       protected void DrawAxis(PaintEventArgs e)
       {

           //if (this._tempLayer != null)
           //{

           //    if (this.m_ParenLManager.CurrentMap.MapFrame.DrawingLayers.Contains(this._tempLayer))
           //    {
           //        this.m_ParenLManager.CurrentMap.MapFrame.DrawingLayers.Remove(this._tempLayer);
           //    }
           //}

           System .Drawing .Point pp1 = this.m_ParenLManager.CurrentMap.ProjToPixel(new Coordinate(0, 0));
           System.Drawing.Point pp2 = this.m_ParenLManager.CurrentMap.ProjToPixel(new Coordinate(300, 300));


           g.DrawLine(System.Drawing.Pens.Red, pp1, pp2);
           this.m_ParenLManager.CurrentMap.MapFrame.Print(g, r);
          

           //所有横线左侧X坐标
           double LeftX = this._LeftBottomX + this._OffsetX;
           //所有横线右侧X坐标
           double RightX = this._LeftBottomX + this._OffsetX + this._XUnit * this._XCells;
           //所有纵线底部Y坐标
           double BottomY = this._LeftBottomY + this._OffsetY;
           //所有纵线顶部Y坐标
           double TopY = this._LeftBottomY + this._OffsetY + this._YUnit * this._YCells;

           //(this .ILayer as DotSpatial .Symbology .IFeatureLayer ).MapFrame .Extents 

           //左侧坐标轴
           if (this._IsShowLeftAxis == true && this.m_bVisiable == true)
           {
               for (int i = 0; i <= this._YCells; i++)
               {
                   int result = 0;

                   //主刻度
                   Math.DivRem(i, this._mainScaleStep, out result);
                   if (result == 0)
                   {
                       Coordinate c1 = new Coordinate(LeftX, BottomY + this._YUnit * i);
                       Coordinate c2 = new Coordinate(LeftX - this._mainScaleLength, BottomY + this._YUnit * i);
                       System.Drawing.PointF p1 = this.m_ParenLManager.CurrentMap.ProjToPixel(c1);
                       System.Drawing.PointF p2 = this.m_ParenLManager.CurrentMap.ProjToPixel(c2);

                       e.Graphics.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Black, 1), p1, p2);
                       System.Drawing.StringFormat format = new System.Drawing.StringFormat();
                       format.Alignment = System.Drawing.StringAlignment.Center;
                       e.Graphics.DrawString((this._LeftBottomY + i * this._YUnit).ToString(), new System.Drawing.Font("Times New Roman", 12), System.Drawing.Brushes.Black, p2.X - 30, p2.Y - 10, format);
                   }
                   //副刻度
                   Math.DivRem(i, this._viceScaleStep, out result);
                   if (result == 0)
                   {
                       Coordinate c1 = new Coordinate(LeftX, BottomY + this._YUnit * i);
                       Coordinate c2 = new Coordinate(LeftX - this._viceScaleLength, BottomY + this._YUnit * i);

                       System.Drawing.PointF p1 = this.m_ParenLManager.CurrentMap.ProjToPixel(c1);
                       System.Drawing.PointF p2 = this.m_ParenLManager.CurrentMap.ProjToPixel(c2);

                       e.Graphics.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Black, 1), p1, p2);
                   }
               }

           }


           //底部坐标轴
           if (this._IsShowBottomAxis == true && this.m_bVisiable == true)
           {
               for (int i = 0; i <= this._XCells; i++)
               {
                   int result = 0;

                   //主刻度
                   Math.DivRem(i, this._mainScaleStep, out result);
                   if (result == 0)
                   {
                       Coordinate c1 = new Coordinate(LeftX + this._XUnit * i, BottomY);
                       Coordinate c2 = new Coordinate(LeftX + this._XUnit * i, BottomY - this._mainScaleLength);
                       System.Drawing.PointF p1 = this.m_ParenLManager.CurrentMap.ProjToPixel(c1);
                       System.Drawing.PointF p2 = this.m_ParenLManager.CurrentMap.ProjToPixel(c2);

                       e.Graphics.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Black, 1), p1, p2);
                       e.Graphics.DrawString((this._LeftBottomX + i * this._XUnit).ToString(), new System.Drawing.Font("Times New Roman", 12), System.Drawing.Brushes.Black, p2.X - 20, p2.Y);
                       //e.Graphics .DrawString (
                   }

                   //副刻度
                   Math.DivRem(i, this._viceScaleStep, out result);
                   if (result == 0)
                   {
                       Coordinate c1 = new Coordinate(LeftX + this._XUnit * i, BottomY);
                       Coordinate c2 = new Coordinate(LeftX + this._XUnit * i, BottomY - this._viceScaleLength);

                       System.Drawing.Point p1 = this.m_ParenLManager.CurrentMap.ProjToPixel(c1);
                       System.Drawing.Point p2 = this.m_ParenLManager.CurrentMap.ProjToPixel(c2);

                       e.Graphics.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Black, 1), p1, p2);
                   }
               }
           }


           //右侧坐标轴
           if (this._IsShowRightAxis == true)
           {

               for (int i = 0; i <= this._YCells; i++)
               {
                   int result = 0;

                   //主刻度
                   Math.DivRem(i, this._mainScaleStep, out result);
                   if (result == 0)
                   {
                       Coordinate c1 = new Coordinate(RightX, BottomY + this._YUnit * i);
                       Coordinate c2 = new Coordinate(RightX + this._mainScaleLength, BottomY + this._YUnit * i);
                       System.Drawing.PointF p1 = this.m_ParenLManager.CurrentMap.ProjToPixel(c1);
                       System.Drawing.PointF p2 = this.m_ParenLManager.CurrentMap.ProjToPixel(c2);

                       e.Graphics.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Black, 1), p1, p2);
                       System.Drawing.StringFormat format = new System.Drawing.StringFormat();
                       format.Alignment = System.Drawing.StringAlignment.Center;
                       e.Graphics.DrawString((this._LeftBottomY + i * this._YUnit).ToString(), new System.Drawing.Font("Times New Roman", 12), System.Drawing.Brushes.Black, p2.X + 30, p2.Y - 10, format);
                   }
                   //副刻度
                   Math.DivRem(i, this._viceScaleStep, out result);
                   if (result == 0)
                   {
                       Coordinate c1 = new Coordinate(RightX, BottomY + this._YUnit * i);
                       Coordinate c2 = new Coordinate(RightX + this._viceScaleLength, BottomY + this._YUnit * i);

                       System.Drawing.Point p1 = this.m_ParenLManager.CurrentMap.ProjToPixel(c1);
                       System.Drawing.Point p2 = this.m_ParenLManager.CurrentMap.ProjToPixel(c2);

                       e.Graphics.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Black, 1), p1, p2);
                   }
               }

           }


           //顶部坐标轴
           if (this._IsShowUpAxis == true)
           {

               for (int i = 0; i <= this._XCells; i++)
               {
                   int result = 0;

                   //主刻度
                   Math.DivRem(i, this._mainScaleStep, out result);
                   if (result == 0)
                   {
                       Coordinate c1 = new Coordinate(LeftX + this._XUnit * i, TopY);
                       Coordinate c2 = new Coordinate(LeftX + this._XUnit * i, TopY + this._mainScaleLength);
                       System.Drawing.Point p1 = this.m_ParenLManager.CurrentMap.ProjToPixel(c1);
                       System.Drawing.Point p2 = this.m_ParenLManager.CurrentMap.ProjToPixel(c2);

                       e.Graphics.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Black, 1), p1, p2);
                       e.Graphics.DrawString((this._LeftBottomX + i * this._XUnit).ToString(), new System.Drawing.Font("Times New Roman", 12), System.Drawing.Brushes.Black, p2.X - 20, p2.Y - 20);
                       //e.Graphics .DrawString (
                   }

                   //副刻度
                   Math.DivRem(i, this._viceScaleStep, out result);
                   if (result == 0)
                   {
                       Coordinate c1 = new Coordinate(LeftX + this._XUnit * i, TopY);
                       Coordinate c2 = new Coordinate(LeftX + this._XUnit * i, TopY + this._viceScaleLength);

                       System.Drawing.Point p1 = this.m_ParenLManager.CurrentMap.ProjToPixel(c1);
                       System.Drawing.Point p2 = this.m_ParenLManager.CurrentMap.ProjToPixel(c2);

                       e.Graphics.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Black, 1), p1, p2);
                   }
               }


           }


           //this._tempLayer = new DotSpatial.Controls.MapLineLayer(this.DataSet as IFeatureSet)
           //{
           //    Symbolizer =
           //    {
           //        ScaleMode = DotSpatial.Symbology.ScaleMode.Symbolic,
           //        Smoothing = true
           //    },
           //    MapFrame = this.m_ParenLManager.CurrentMap.MapFrame
           //};

           (this .ILayer as HAMapLineLayer ).DrawFeatures 

           //this.m_ParenLManager.CurrentMap.MapFrame. = this.DataSet;
           //this.m_ParenLManager.CurrentMap.MapFrame.DrawingLayers.Add(this._tempLayer);

           //this.m_ParenLManager.CurrentMap.MapFrame.Invalidate();
           //this.m_ParenLManager.CurrentMap.Invalidate();

       }

      protected void  DrawAxisText()
      {

      }

      public void SetGridProperty(int XCells, int YCells, double LeftBottomX, double LeftBottomY,
          double XUnit, double YUnit, double OffsetX, double OffsetY)
      {
          this._XCells = XCells;
          this._YCells = YCells;
          this._LeftBottomX = LeftBottomX;
          this._LeftBottomY = LeftBottomY;
          this._XUnit = XUnit;
          this._YUnit = YUnit;
          this._OffsetX = OffsetX;
          this._OffsetY = OffsetY;

          if (this.BRunning)
              this.Redraw();
      }


      protected  override void BuildLayer()
      {
          base.BuildLayer();

          this.m_ParenLManager.CurrentMap.Paint += new PaintEventHandler(this.OnPaint);
         
      }

      private void OnPaint(object sender, PaintEventArgs e)
      {
          this.DrawAxis(e);

          //this.m_ParenLManager.CurrentMap.Update();
      }
        
     


       //根据属性进行重新绘制
       protected  override void Redraw()
       {

           (this.ILayer.DataSet as IFeatureSet).UpdateExtent();


           (this.ILayer.DataSet as IFeatureSet).Features.Clear();

           this.DrawGridLine();
           //this.DrawAxis();
           //this.DrawAxisText();
                               

           (this.ILayer.DataSet as IFeatureSet).InvalidateVertices();
           (this.DataSet as FeatureSet).InvalidateEnvelope();
           //(this.ILayer.DataSet as IFeatureSet).UpdateExtent();

           //this.m_ParenLManager.CurrentMap.Invalidate();

           this.m_ParenLManager.CurrentMap.Update();

       }

       public override void CloseLayer()
       {
           base.CloseLayer();
           this._IsShowLeftAxis = false;
           this._IsShowRightAxis = false;
           this._IsShowUpAxis = false;
           this._IsShowBottomAxis = false;
           
       }

    }
}
